<template>
  <div class="topnav"
    :class="{'doc-nav-style': toggleMenuVisible}">
    <router-link to="/" class="logo">
      <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-logo"></use>
      </svg>
    </router-link>
    <ul class="menu">
      <li @click="setLang('en')"
        :class="{selected: $i18n.locale === 'en'}">
        En
      </li>
      <li @click="setLang('zh')"
        :class="{selected: $i18n.locale === 'zh'}">
        中
      </li>
    </ul>
    <svg v-if="toggleMenuVisible"
      @click="toggleMenu" class="toggleAsideMenu"
      aria-hidden="true">
      <use xlink:href="#icon-menu"></use>
    </svg>
  </div>
</template>

<script lang="ts">
import { inject } from 'vue'
import { MENU_VISIBLE, STORAGENAME_LANG } from '../constants/Refs'
import i18n from '../i18n'

export default {
  props: {
    toggleMenuVisible: {
      type: Boolean,
      default: false
    }
  },
  setup() {
    const menuVisible = inject<Ref<boolean>>(MENU_VISIBLE)
    const toggleMenu = () => {
      menuVisible.value = !menuVisible.value
    }
    const setLang = (lang) => {
      i18n.global.locale.value = lang
      window.localStorage.setItem(STORAGENAME_LANG, lang)
    }
    return { toggleMenu, setLang }
  }
}
</script>

<style lang="scss" scoped>
$color: #56658e;
.topnav {
  display: flex;
  padding: 16px;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 10;
  justify-content: center;
  align-items: center;
  color: $color;
  > .logo {
    margin-right: auto;
    .icon {
      width: 32px;
      height: 32px;
    }
  }
  > .menu {
    display: flex;
    white-space: nowrap;
    flex-wrap: nowrap;
    > li {
      line-height: 32px;
      padding: 0 8px;
      margin: 0 4px;
      border-radius: 4px;
      cursor: pointer;
      &.selected {
        background: $color;
        color: #fff;
      }
    }
  }
  > .toggleAsideMenu {
    width: 24px;
    height: 24px;
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    fill: $color;
    display: none;
  }
}
@media (max-width: 500px) {
  .topnav.doc-nav-style {
    justify-content: flex-end;
    > .menu > li {
      margin: 0;
    }
    > .logo {
      position: absolute;
      left: 50%;
      margin-left: -16px;
    }
    > .toggleAsideMenu { display: inline-block; }
  }
}
</style>
